package view.admin.productMsgManage;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import controller.ProductMsgController;
import dao.ProductMsgDao;
import dao.ProductTypeDao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import pojo.equip.EquipTypeEntity;
import pojo.product.ProductMsgEntity;
import pojo.product.ProductTypeEntity;
import view.Main;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Limohan
 */
public class ChangeProductController {
    private Main myApp;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private String id;
    public ChangeProductController() {
    }
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public void setId(String id){
        this.id = id;
    }

    @FXML
    private JFXTextField name;

    @FXML
    private JFXComboBox<String> type;

    @FXML
    private JFXTextField mode;

    @FXML
    private JFXTextField detail;
    
    @FXML
    void initialize() throws IOException {
        ProductTypeDao productTypeDao = new ProductTypeDao();
        for (ProductTypeEntity productTypeEntity : productTypeDao.findUserList()) {
            type.getItems().add(productTypeEntity.getType());
        }
    }

    @FXML
    void returnMsgMangePage(ActionEvent event) throws Exception {
        myApp.goToProductMsgManagePage();
    }

    @FXML
    void saveNewProduct(ActionEvent event) throws IOException {
        ProductMsgDao productMsgDao = new ProductMsgDao();
        productMsgDao.deleteMsg(id);
        ProductMsgEntity productMsgEntity = new ProductMsgEntity();
        ProductMsgController productMsgController = ProductMsgController.getInstance();
        Date date = new Date();
        productMsgEntity.setCode("PD"+sdf.format(date));
        productMsgEntity.setDetail(detail.getText());
        productMsgEntity.setMode(mode.getText());
        productMsgEntity.setType(type.getValue());
        productMsgEntity.setName(name.getText());
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(productMsgController.addMsg(productMsgEntity));
        alert.showAndWait();
    }

    public void originDisplay() throws IOException {
        ProductMsgDao productMsgDao = new ProductMsgDao();
        ProductMsgEntity productMsgEntity = productMsgDao.findMsg(id);
        name.setText(productMsgEntity.getName());
        type.setValue(productMsgEntity.getType());
        mode.setText(productMsgEntity.getMode());
        detail.setText(productMsgEntity.getDetail());
    }

}
